/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-02-20 12:34:54
 * @LastEditors: Clark
 * @LastEditTime: 2023-02-20 12:43:06
 * @Description: file content
 */

#include "opend.h"
#include <fcntl.h>

void handle_request(char *buf, int nread, int fd)
{
    int newfd = -1;

    if (0 != buf[nread -1])
    {
        snprintf(errmsg, MAXLINE-1, "request not null terminated: %*.*s\n", uid, nread, nread, buf);
        send_err(fd, -1, errmsg);
        return;
    }

    // parse the arguments, set options
    if (buf_args(buf, cli_args) < 0)
    {
        send_err(clifd, -1, errmsg);
        send_err(fd, -1, errmsg);
        return;
    }

    if ((newfd = open(pathname, oflag)) < 0)
    {
        snprintf(errmsg, MAXLINE-1, "can't open: %s: %s\n", pathname, strerror(errno));
        send_err(fd, -1, errmsg);
        return;
    }

    // send the descriptor
    if (send_fd(fd, newfd) < 0)
    {
        err_sys("send_fd error");
    }

    close(newfd);
}
